package com.skyjoo.search.lucene.index.writer.impl;

import java.io.IOException;

import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.IndexWriter;

import com.skyjoo.search.lucene.index.directory.IndexDirectory;
import com.skyjoo.search.lucene.index.directory.IndexResources;
import com.skyjoo.search.lucene.index.writer.AbstractIndexBuilder;
import com.skyjoo.search.lucene.index.writer.CreateIndexMode;

public class DefaultIndexBuilder extends AbstractIndexBuilder {

    public DefaultIndexBuilder(IndexDirectory directory) {
        super(directory);
    }

    @Override
    protected boolean doBeforeIndex(CreateIndexMode mode) {
        try {
            if (IndexWriter.isLocked(getDirectory())) {
                log.error("(" + mode.name + ") current directory is locked.");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException("(" + mode.name + ") check isLocked error, dir=" + getDirectory(), e);
        }
        return true;
    }

   

    @Override
    protected IndexResources getIndexResources(CreateIndexMode mode) {
        return null;
    }

    @Override
    protected void doFinally(IndexWriter indexWriter, DirectoryTaxonomyWriter taxonomyWriter, CreateIndexMode mode) {
        closeIndexWriter(indexWriter);
        closeTaxonomyWriter(taxonomyWriter);
    }

    // @Override
    protected IndexWriter getIndexWriter() {
        return createIndexWriter();
    }

    @Override
    protected IndexWriter getIndexWriter(CreateIndexMode mode) {
        return getIndexWriter();
    }

}
